package com.fira.edu.service.impl;

import java.util.List;

import com.fira.common.constant.Constants;
import com.fira.common.utils.DateUtils;
import com.fira.common.utils.StringUtils;
import com.fira.common.utils.bean.DozerUtils;
import com.fira.common.utils.uuid.IdUtils;
import com.fira.edu.domain.dto.CourseScheduleDTO;
import com.fira.edu.domain.dto.parent.ClassScheduleListDTO;
import com.fira.edu.domain.vo.ClassScheduleListVO;
import com.fira.edu.mapper.EduCourseScheduleTimeMapper;
import com.fira.edu.service.IEduCourseScheduleTimeService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fira.edu.mapper.EduCourseScheduleMapper;
import com.fira.edu.domain.EduCourseSchedule;
import com.fira.edu.service.IEduCourseScheduleService;
import com.fira.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程排课Service业务层处理
 * 
 * @author zhushuyong
 * @date 2021-10-12
 */
@Service
public class EduCourseScheduleServiceImpl implements IEduCourseScheduleService 
{
    @Autowired
    private EduCourseScheduleMapper eduCourseScheduleMapper;
    @Autowired
    private IEduCourseScheduleTimeService eduCourseScheduleTimeService;
    /**
     * 查询课程排课
     * 
     * @param id 课程排课ID
     * @return 课程排课
     */
    @Override
    public EduCourseSchedule selectEduCourseScheduleById(String id)
    {
        return eduCourseScheduleMapper.selectEduCourseScheduleById(id);
    }

    /**
     * 查询课程排课列表
     * 
     * @param eduCourseSchedule 课程排课
     * @return 课程排课
     */
    @Override
    public List<EduCourseSchedule> selectEduCourseScheduleList(EduCourseSchedule eduCourseSchedule)
    {
        return eduCourseScheduleMapper.selectEduCourseScheduleList(eduCourseSchedule);
    }

    /**
     * 新增课程排课
     * 
     * @param courseScheduleDTO 课程排课
     * @return 结果
     */
    @Override
    @Transactional
    public int insertEduCourseSchedule(CourseScheduleDTO courseScheduleDTO)
    {
        courseScheduleDTO.setId(IdUtils.fastSimpleUUID());
        EduCourseSchedule eduCourseSchedule = DozerUtils.copyProperties(courseScheduleDTO,EduCourseSchedule.class);
        eduCourseSchedule.setCreateTime(DateUtils.getNowDate());
        int rows = eduCourseScheduleMapper.insertEduCourseSchedule(eduCourseSchedule);
        //插入排课时间数据
        if(CollectionUtils.isNotEmpty(courseScheduleDTO.getCourseScheduleTimeDTOList())){
            courseScheduleDTO.getCourseScheduleTimeDTOList().forEach(item -> {
                item.setCourseScheduleId(eduCourseSchedule.getId());
                if(StringUtils.isEmpty(item.getCourseStatus())){
                    item.setCourseStatus(Constants.STATUS_ZERO);
                }
            });
            eduCourseScheduleTimeService.batchInsert(courseScheduleDTO.getCourseScheduleTimeDTOList());
        }
        return rows;
    }

    /**
     * 修改课程排课
     * 
     * @param eduCourseSchedule 课程排课
     * @return 结果
     */
    @Override
    public int updateEduCourseSchedule(EduCourseSchedule eduCourseSchedule)
    {
        eduCourseSchedule.setUpdateTime(DateUtils.getNowDate());
        return eduCourseScheduleMapper.updateEduCourseSchedule(eduCourseSchedule);
    }

    /**
     * 删除课程排课对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteEduCourseScheduleByIds(String ids)
    {
        return eduCourseScheduleMapper.deleteEduCourseScheduleByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程排课信息
     * 
     * @param id 课程排课ID
     * @return 结果
     */
    @Override
    public int deleteEduCourseScheduleById(String id)
    {
        return eduCourseScheduleMapper.deleteEduCourseScheduleById(id);
    }

    /**
     * 小程序-查询课表列表
     *
     * @param classScheduleListDTO
     * @return
     */
    @Override
    public List<ClassScheduleListVO> selectCourseScheduleList(ClassScheduleListDTO classScheduleListDTO) {
        return eduCourseScheduleMapper.selectCourseScheduleList(classScheduleListDTO);
    }
}
